/**
 * 通用js封装
 * Author: Alex
 * Date: 2021/8/15
 */


(function ($) {
    $.extend({
        operate:{
            submit: function(url, type, dataType, data, callback) {
                let loadingIndex;
                var config = {
                    url: url,
                    type: type,
                    dataType: dataType,
                    data: data,
                    beforeSend: function () {
                        loadingIndex = $.modal.loading("正在处理中，请稍后...");
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.modal.closeLoading(loadingIndex);
                    }
                };
                $.ajax(config)
            },
            post: function(url, data, callback) {
                $.operate.submit(url, "post", "json", data, callback);
            },
            get: function(url, callback) {
                $.operate.submit(url, "get", "json", "", callback);
            },
        },
        modal: {
            loading: function (message) {
                var loading = layer.msg(message, {
                    icon: 16,
                    shade: [0.5, '#000'],
                    time: false
                });
                return loading;
            },
            closeLoading: function (loadingIndex) {
                layer.close(loadingIndex);
            },
            icon: function(type) {
                var icon = "";
                if (type == web_status.WARNING) {
                    icon = 0;
                } else if (type == web_status.SUCCESS) {
                    icon = 1;
                } else if (type == web_status.FAIL) {
                    icon = 2;
                } else {
                    icon = 3;
                }
                return icon;
            },
            msg: function(content, type) {
                if (type != undefined) {
                    layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
                } else {
                    layer.msg(content);
                }
            },
            msgError: function(content) {
                $.modal.msg(content, web_status.FAIL);
            },
            msgSuccess: function(content) {
                $.modal.msg(content, web_status.SUCCESS);
            },
            msgWarning: function(content) {
                $.modal.msg(content, web_status.WARNING);
            },
            alert: function(content, type) {
                layer.alert(content, {
                    icon: $.modal.icon(type),
                    title: "系统提示",
                    btn: ['确认'],
                    btnclass: ['btn btn-primary'],
                });
            }
        }
    })
})(jQuery);



/** 消息状态码 */
web_status = {
    SUCCESS: 200,
    FAIL: 500,
    WARNING: 301
};

/** 弹窗状态码 */
modal_status = {
    SUCCESS: "success",
    FAIL: "error",
    WARNING: "warning"
};